//
//  GridMeViewCell.swift
//  grid-ios-app
//
//  Created by yanlu on 2024/3/23.
//

import UIKit
import SnapKit

class GridMeHeaderCell: UITableViewCell {
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        setupLayouts()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupLayouts() {
        contentView.addSubview(headerImageView)
        contentView.addSubview(titleLabel)
        contentView.addSubview(messageLabel)
        contentView.addSubview(lineView)
        
       
        
        headerImageView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(12)
            make.centerY.equalToSuperview()
            make.width.equalTo(50)
            make.height.equalTo(50)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(headerImageView.snp.right).offset(10)
            make.top.equalTo(headerImageView.snp.top).offset(6)
            make.right.equalToSuperview().offset(-12)
            make.height.equalTo(20)
        }
        messageLabel.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel.snp.left)
            make.right.equalTo(titleLabel.snp.right)
            make.top.equalTo(titleLabel.snp.bottom).offset(2)
            make.height.equalTo(20)
        }
        lineView.snp.makeConstraints { (make) in
            make.bottom.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
    }
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "头像"
        label.backgroundColor = .clear
        return label
    }()
    
    lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(10)
        label.text = "信息"
        return label
    }()
    
    lazy var headerImageView: UIImageView = {
        let view = UIImageView(image: UIImage(named: "Mine_UnLogin"))
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 25.0
        return view
    }()
    
    
}


class GridMeMessageCell: UITableViewCell {
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        setupLayouts()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupLayouts() {
       
        rightImageView.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-12)
            make.centerY.equalToSuperview()
            make.width.equalTo(8)
            make.height.equalTo(14)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-100)
            make.height.equalTo(20)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.bottom.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
    }
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        contentView.addSubview(view)
        return view
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "App检查更新"
        label.backgroundColor = .clear
        contentView.addSubview(label)
        return label
    }()
    
    lazy var rightImageView: UIImageView = {
        let view = UIImageView(image: UIImage(named: "Mine_More"))
        contentView.addSubview(view)
        return view
    }()
}

class GridMeUpdateCell: UITableViewCell {
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        setupLayouts()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupLayouts() {
        
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(6)
            make.right.equalToSuperview().offset(-100)
            make.height.equalTo(20)
        }
        messageLabel.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel.snp.left)
            make.right.equalTo(titleLabel.snp.right)
            make.top.equalTo(titleLabel.snp.bottom).offset(2)
            make.height.equalTo(20)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.bottom.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        loginBtn.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-12)
            make.centerY.equalToSuperview()
            make.width.equalTo(60)
            make.height.equalTo(20)
        }
    }
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        contentView.addSubview(view)
        return view
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "App检查更新"
        label.backgroundColor = .clear
        contentView.addSubview(label)
        return label
    }()
    
    lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(10)
        let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String
        let build = Bundle.main.infoDictionary?["CFBundleVersion"] as! String
        label.text = "当前版本：\(version)(\(build))"
        contentView.addSubview(label)
        return label
    }()
    
    lazy var loginBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.setTitle(NSLocalizedString("更新", tableName: "GridLocalizable", comment: ""), for: .normal)
        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
        button.titleLabel!.font = Theme.Font.size(10)
        button.addTarget(self, action: #selector(onClickButton), for: .touchUpInside)
        button.backgroundColor  = UIColor(hex6: 0x1E90FF)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 10
        contentView.addSubview(button)
        return button
    }()
    
    @objc func onClickButton() {
        print("更新逻辑")
    }
}

// 退出登录
class GridMeLoginOutView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    
    private func setupUI() {
        
        addSubview(loginOutBtn)
        loginOutBtn.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.right.equalToSuperview().offset(-20)
            make.left.equalToSuperview().offset(20)
            make.height.equalTo(40)
        }
    }
    
    lazy var loginOutBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.setTitle(NSLocalizedString("退出登录", tableName: "GridLocalizable", comment: ""), for: .normal)
        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
        button.titleLabel!.font = Theme.Font.size(18, bold: true)
        button.addTarget(self, action: #selector(onClickButton), for: .touchUpInside)
        button.backgroundColor  = UIColor(hex6: 0x1E90FF)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 20
        return button
    }()
    
    @objc func onClickButton() {
        print("退出登录逻辑")
        GridLoginManager.shared.removeToken()
        if UIApplication.shared.delegate != nil {
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            appDelegate.resetRootViewController()
        }
    }
}
